import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {TipService} from "../../services/tip.service";
import {Router} from "@angular/router";
import {ExtendHttpService} from "../../../common/extend-http.service";

@Component({
  selector: 'app-waiter-count-details',
  templateUrl: './waiter-count-details.component.html',
  styleUrls: ['./waiter-count-details.component.css']
})
export class WaiterCountDetailsComponent implements OnInit {
  @Input() uid = '';
  @Output() closeDetail = new EventEmitter()

  loading = true;
  isFlag = false;
  isFlagMore = false;
  tipType = 'error';
  totalPage = 1;
  loadMoreFlag = true;
  list = [];
  params = {startTime: '', endTime: ''};

  // 详情参数
  detailParams = {
    startTime: '',
    endTime: '',
    uid: '',
    start: 1
  };
  detailList = [];

  // 日志记录
  logList = [];

  // 支付方式
  optList = [];

  // 查看详情的参数
  isLoading = false; //  背景遮罩
  detailsShow = false; // 是否展示详情页
  details: any; // 基本信息
  odList: any; // 已点菜品
  detailsFlag = true;

  constructor(private extendHttp: ExtendHttpService,
              private router: Router,
              private tipService: TipService) {
  }

  ngOnInit() {
    // input为date的元素的特殊处理 格式为: 2018-02-25
    var nowEnd = new Date();
    var monthEnd = (nowEnd.getMonth() + 1);
    var day = nowEnd.getDate();
    var m = [];
    if (monthEnd < 10) {
      m.push(0);
      m.push(monthEnd);
    } else {
      m.push(monthEnd);
    }
    var d = []
    if (day < 10) {
      d.push(0);
      d.push(day);
    } else {
      d.push(day);
    }
    var today = nowEnd.getFullYear() + '-' + m.join('') + '-' + d.join('');
    this.params.endTime = today;

    var nowStart = new Date();
    nowStart.setMonth(nowStart.getMonth() - 1);
    var monthStart = nowStart.getMonth() + 1;
    var dayStart = nowStart.getDate();

    var m1 = [];
    var d1 = [];
    if (monthStart < 10) {
      m1.push(0);
      m1.push(monthStart);
    }
    if (dayStart < 10) {
      d1.push(0);
      d1.push(dayStart);
    } else {
      d1.push(dayStart)
    }
    var todayStart = nowEnd.getFullYear() + '-' + m1.join('') + '-' + d1.join('');
    this.params.startTime = todayStart;
    cBack = () => {
      $('#btn-back').trigger('click');
    }
    this.getDetails(false,this.uid);
  }

  loadMore() {

    if (this.loadMoreFlag) {
      this.detailParams.start = this.detailParams.start + 1;
      this.loadMoreFlag = false;
      this.extendHttp.post('/merchantApp/Statistics/findFoodSaleNum', this.detailParams).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.detailList = this.detailList.concat(data.page.list);
          this.detailParams.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.loadMoreFlag = true;
        } else if (data.code == '-2001') {
          this.tipType = 'error';
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.detailParams.start = this.detailParams.start - 1;
          this.tipType = 'error';
          this.tipService.change(true, data.message);
          this.loadMoreFlag = true;
        }
      }, error => {
        this.tipType = 'error';
        this.loadMoreFlag = true;
        this.detailParams.start = this.detailParams.start - 1;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }
  }

  clearData1() {
    this.detailParams.startTime = '';
    this.detailParams.endTime = ''
  }

  // 关闭详情页
  detailsClose() {
    this.detailsShow = false;
    this.isLoading = false;
    this.closeDetail.emit(true);
  }

  // 查看详情
  getDetails(flag,uid) {
    if (flag) {
      if (!this.detailParams.startTime.trim()) {
        this.tipService.change(true, '请选择开始时间');
        return;
      }
      if (!this.detailParams.endTime.trim()) {
        this.tipService.change(true, '请选择结束时间');
        return;
      }
    }
    if (this.detailsFlag) {
      this.isLoading = true;
      this.detailsFlag = false;

      // input为date的元素的特殊处理 格式为: 2018-02-25
      var nowEnd = new Date();
      var monthEnd = (nowEnd.getMonth() + 1);
      var day = nowEnd.getDate();
      var m = [];
      if (monthEnd < 10) {
        m.push(0);
        m.push(monthEnd);
      } else {
        m.push(monthEnd);
      }
      var d = []
      if (day < 10) {
        d.push(0);
        d.push(day);
      } else {
        d.push(day);
      }
      var today = nowEnd.getFullYear() + '-' + m.join('') + '-' + d.join('');
      if (!this.detailParams.endTime) {
        this.detailParams.endTime = today;
      }


      var nowStart = new Date();
      nowStart.setMonth(nowStart.getMonth() - 1);
      var monthStart = nowStart.getMonth() + 1;
      var dayStart = nowStart.getDate();

      var m1 = [];
      var d1 = [];
      if (monthStart < 10) {
        m1.push(0);
        m1.push(monthStart);
      }
      if (dayStart < 10) {
        d1.push(0);
        d1.push(dayStart);
      } else {
        d1.push(dayStart)
      }
      var todayStart = nowEnd.getFullYear() + '-' + m1.join('') + '-' + d1.join('');
      if (!this.detailParams.startTime) {
        this.detailParams.startTime = todayStart;
      }
      this.detailParams.uid = uid;
      this.extendHttp.post('/merchantApp/Statistics/findFoodSaleNum', this.detailParams).subscribe(response => {
        let data = response.json();
        if (data.code == '0') {
          this.isFlagMore = true;
          this.isLoading = false;
          this.detailList = data.page.list;
          this.detailParams.start = data.page.pageNumber;
          this.totalPage = data.page.totalPage;
          this.detailsShow = true;
          this.detailsFlag = true;
        } else if (data.code == '-2001') {
          this.isLoading = false;
          this.tipType = 'error';
          this.detailsFlag = true;
          this.tipService.change(true, '登录超时，跳转到登录页面');
          setTimeout(() => {
            this.router.navigate(['/login']);
          }, 2000);
        } else {
          this.tipType = 'error';
          this.isLoading = false;
          this.detailsFlag = true;
          this.tipService.change(true, data.message);
        }
      }, error => {
        this.tipType = 'error';
        this.isLoading = false;
        this.detailsFlag = true;
        this.tipService.change(true, '系统繁忙，操作失败！');
      });
    }

  }

}
